package com.hckj.jianzhijia;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avoscloud.leanchatlib.activity.AVChatActivity;
import com.hckj.jianzhijia.listeners.OnBackClickListener;
import com.hckj.jianzhijia.model.City;
import com.hckj.jianzhijia.model.NormalUser;
import com.hckj.jianzhijia.model.Province;
import com.hckj.jianzhijia.model.Schools;
import com.hckj.jianzhijia.utils.Data;
import com.hckj.jianzhijia.utils.ExceptionUtils;
import com.hckj.jianzhijia.utils.ToastUtils;
import com.hckj.jianzhijia.views.RoundImageView;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class EnrollPeopleInfoActivity extends BaseActivity {


    @InjectView(R.id.imgv_go_back)
    ImageView imgvGoBack;
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.rl_toolbar)
    RelativeLayout rlToolbar;
    @InjectView(R.id.imgv_enroller_pic)
    RoundImageView imgvEnrollerPic;
    @InjectView(R.id.tv_enroller_name)
    TextView tvEnrollerName;
    @InjectView(R.id.tv_address)
    TextView tvAddress;
    @InjectView(R.id.tv_school)
    TextView tvSchool;
    @InjectView(R.id.tv_gender)
    TextView tvGender;
    @InjectView(R.id.tv_age)
    TextView tvAge;
    @InjectView(R.id.tv_intent)
    TextView tvIntent;
    @InjectView(R.id.tv_experience)
    TextView tvExperience;
    @InjectView(R.id.tv_phone)
    TextView tvPhone;
    @InjectView(R.id.rl_phone)
    RelativeLayout rlPhone;
    @InjectView(R.id.tv_email)
    TextView tvEmail;
    @InjectView(R.id.rl_email)
    RelativeLayout rlEmail;
    @InjectView(R.id.tv_chat)
    TextView tvChat;
    @InjectView(R.id.rl_chat)
    RelativeLayout rlChat;
    @InjectView(R.id.tv_message)
    TextView tvMessage;
    @InjectView(R.id.rl_message)
    RelativeLayout rlMessage;

    private NormalUser normalUser;
    private ProgressDialog progressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enroll_people_info);
        ButterKnife.inject(this);
        normalUser = (NormalUser) getIntent().getParcelableExtra("NormalUser");
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("加载中...");
        progressDialog.setCancelable(false);
        GetInfoNetTask getInfoNetTask = new GetInfoNetTask();
        getInfoNetTask.execute();
    }

    private void initData() {

        tvTitle.setText(normalUser.getName());
        if(normalUser.getPic()==null || normalUser.getPic().getUrl()==null){
            imgvEnrollerPic.setImageResource(R.drawable.icon_default_head_pic);
        }else{
            Picasso.with(this).load(normalUser.getPic().getUrl()).into(imgvEnrollerPic);
        }

        tvEnrollerName.setText(normalUser.getName());
        //少了区的信息
        tvAddress.setText( normalUser.getProvinceName() + normalUser.getCityName());
        tvSchool.setText(normalUser.getSchoolName());
        tvGender.setText(normalUser.getSex()==0? "性别:男":"性别:女");

        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date currentDate=new Date();
        java.util.Date birthdayDate= normalUser.getBirthday();
        long day=(currentDate.getTime()-birthdayDate.getTime())/(24*60*60*1000) + 1;
        String year=(int)(day/365f)+"";
        tvAge.setText("年龄:" + year + "岁");
        String jobIntent = "";
        for (int i = 0,j = normalUser.getIntentionArray().length; i < j; i++) {
            if(i==3){
                jobIntent = "\n" + jobIntent;
            }
            jobIntent += Data.JobTypeData[normalUser.getIntentionArray()[i]] + "\t";
        }
        tvIntent.setText(jobIntent);
        tvExperience.setText(normalUser.getExperience());
        tvPhone.setText(normalUser.getUsername());
        tvEmail.setText(normalUser.getEmail());
        tvChat.setText("点击聊天");
        tvMessage.setText("点击发送短信");

    }

    private void initEvent() {

        imgvGoBack.setOnClickListener(new OnBackClickListener(this));
        rlPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + normalUser.getUsername()));
                startActivity(intent);
            }
        });

        rlEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent data=new Intent(Intent.ACTION_SENDTO);
                data.setData(Uri.parse("mailto:" + normalUser.getEmail()));
                data.putExtra(Intent.EXTRA_SUBJECT, "兼职家报名回复");
                data.putExtra(Intent.EXTRA_TEXT, "您好，感谢您通过兼职家报名我们的兼职....");
                startActivity(data);
            }
        });

        rlChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(getApplicationContext(), AVChatActivity.class);
//                Bundle bundle = new Bundle();
//                bundle.putString(Data.TALK_ID, normalUser.getName() + "#" + normalUser.getUsername());
//                intent.putExtras(bundle);
//                startActivity(intent);
                ToastUtils.showToast(getApplicationContext(),"正在开发中...");
            }
        });

        rlMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("smsto:" + normalUser.getUsername());
                Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
                sendIntent.putExtra("sms_body", "您好，感谢您通过兼职家报名我们的兼职....");
                startActivity(sendIntent);
            }
        });
    }

    class GetInfoNetTask extends AsyncTask<Void,Void,String>{


        public GetInfoNetTask() {
            super();
        }

        @Override
        protected String doInBackground(Void... params) {

            try {
                AVQuery<AVObject> provinceAVQuery = new AVQuery<AVObject>(Province.TABLE_NAME);
                AVQuery<AVObject> cityAVQuery = new AVQuery<AVObject>(City.TABLE_NAME);
                AVQuery<AVObject> schoolQuery = new AVQuery<AVObject>(Schools.TABLE_NAME);
                provinceAVQuery.whereEqualTo("provinceId", normalUser.getProvince());
                cityAVQuery.whereEqualTo("cityId", normalUser.getCity());
                schoolQuery = schoolQuery.whereEqualTo("schoolId", normalUser.getSchool());
                normalUser.setCityName(cityAVQuery.find().get(0).getString("city"));
                normalUser.setSchoolName(schoolQuery.find().get(0).getString("school"));
                normalUser.setProvinceName(provinceAVQuery.find().get(0).getString("province"));
                return null;
            } catch (AVException e) {
                e.printStackTrace();
                return ExceptionUtils.getExceptionInfo(e);
            }
        }

        @Override
        protected void onPostExecute(String s) {

            if(s==null){
                initData();
                initEvent();
                progressDialog.dismiss();
            }else {
                ToastUtils.showToast(getApplicationContext(), s);
                progressDialog.dismiss();
                finish();
            }
            super.onPostExecute(s);
        }

        @Override
        protected void onPreExecute() {
            progressDialog.show();
            super.onPreExecute();
        }
    }




}
